package com.zz.study.sharding.algorithm;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Range;
import com.zz.study.sharding.algorithm.model.TimeRange;
import com.zz.study.sharding.util.DateTool;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * created by xjx on  2023-12-28 17:33
 */
public class RangeShardingAlgorithmTrackData implements RangeShardingAlgorithm<Timestamp> {
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Timestamp> rangeShardingValue) {
        Range<Timestamp> valueRange = rangeShardingValue.getValueRange();
        //开始时间，范围值的上限
       Date dateTimeStart = new Date(((Timestamp)valueRange.lowerEndpoint()).getTime());

        Date dateTimeEnd = new Date(((Timestamp)valueRange.upperEndpoint()).getTime());

        TimeRange timeRangeInput = new TimeRange(dateTimeStart,dateTimeEnd);

        Collection<String> list = new ArrayList<>();
        for (String s : collection) {
            int lastIndex = s.lastIndexOf("_");
            String dateStr = s.substring(lastIndex+1,s.length());
            TimeRange timeRange = convertStr2Range(dateStr);
            if(existInRange(timeRangeInput,timeRange)){
                list.add(s);
            }
        }
        return list;
    }


    public boolean existInRange(TimeRange rangeBig,TimeRange rangeSmall){
        Date startTimeBig = rangeBig.getStartTime();
        Date endTimeBig = rangeBig.getEndTime();

        Date startTimeSmall = rangeSmall.getStartTime();
        Date endTimeSmall = rangeSmall.getEndTime();
        if(startTimeBig.getTime()>startTimeSmall.getTime() && startTimeBig.getTime()<endTimeSmall.getTime()){
            return true;
        }

        if(endTimeBig.getTime()>startTimeSmall.getTime() && endTimeBig.getTime()<endTimeSmall.getTime()){
            return true;
        }

        if(startTimeBig.getTime()>startTimeSmall.getTime() && endTimeBig.getTime()<endTimeSmall.getTime()){
            return true;
        }

        if(startTimeBig.getTime()<startTimeSmall.getTime() && endTimeBig.getTime()>endTimeSmall.getTime()){
            return true;
        }
        return false;
    }

    /**
     *
     * @param str 20239 2023年9月份
     * @return 2023-09-01 00:00:00~2023-10-01 00:00:00
     */
    public static TimeRange convertStr2Range(String str){
        String yearStr = str.substring(0,4);
        String month = str.substring(4,str.length());
        Integer monthNumber = Integer.parseInt(month);
        Integer yearNumber = Integer.parseInt(yearStr);
        Date startTime = DateTool.getMonthBegin(yearNumber,monthNumber);
        Date endTime = new Date();
        if(monthNumber==12){
            endTime = DateTool.getMonthBegin(yearNumber+1,1);
        }else{
            endTime = DateTool.getMonthBegin(yearNumber,monthNumber+1);
        }
        return new TimeRange(startTime,endTime);
    }
}
